
#include "triu_tiling.h"
#include "register/op_def_registry.h"

namespace optiling {

constexpr int32_t CORE_NUM = 1;
constexpr int32_t UB_SIZE = 196608;

constexpr int32_t X_INDEX = 0;
constexpr int32_t DIAGONAL_INDEX = 0;
constexpr int32_t BYTE_BLOCK = 32;
constexpr int32_t BUFFER_NUM = 2;
constexpr int32_t BUF_COUNT = (1 + 1) * BUFFER_NUM;

static ge::graphStatus TilingFunc(gert::TilingContext* context) {
    TriuTilingData tiling;
    uint32_t totalCoreNum = CORE_NUM;
    uint64_t ubSizePlatForm = UB_SIZE;

    const gert::Shape xShape = context->GetInputShape(X_INDEX)->GetStorageShape();
    const ge::DataType dataType = context->GetInputDesc(X_INDEX)->GetDataType();
    int32_t dTypeSize = ge::GetSizeByDataType(dataType);
    int32_t elementsPerBlock = BYTE_BLOCK / dTypeSize;
    int64_t totalDataCount = xShape.GetShapeSize();
    tiling.set_totalDataCount(totalDataCount);

    uint64_t ubMaxProcCount = ubSizePlatForm / BUF_COUNT / BYTE_BLOCK * elementsPerBlock;
    tiling.set_ubMaxProcCount(ubMaxProcCount);

    auto attrs = context->GetAttrs();
    if (attrs == nullptr) {
        return ge::GRAPH_FAILED;
    }

    const int64_t* diagonalPtr = attrs->GetInt(DIAGONAL_INDEX);
    tiling.set_diagonal(*diagonalPtr);

    size_t dimCount = xShape.GetDimNum();
    int64_t colCount = xShape.GetDim(dimCount - 1);
    tiling.set_colCount(colCount);
    tiling.set_rowCount(xShape.GetDim(dimCount - 2));
    int64_t count = 1;
    uint64_t tilingKey = 101;
    if (dimCount > 2) {
        tilingKey = 201;
        for (size_t i = 0; i < dimCount - 2; i++) {
            count *= xShape.GetDim(i);
        }
    }

    if (colCount > ubMaxProcCount) {
        tilingKey += 1;
        tiling.set_innerLoopCount(colCount / ubMaxProcCount);
        tiling.set_innerTailCount(colCount % ubMaxProcCount);
    }
    tiling.set_groupCount(count);

    context->SetBlockDim(totalCoreNum);
    context->SetTilingKey(tilingKey);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

    printf(
        "==zf==Tiling Data, tilingKey:%lu, ubMaxProcCount:%lu, totalDataCount:%lu, tiling.get_groupCount:%lu, "
        "rowCount:%lu, colCount:%lu, innerLoopCount:%lu, innerTailCount:%lu, diagonal:%ld\n",
        tilingKey, tiling.get_ubMaxProcCount(), tiling.get_totalDataCount(), tiling.get_groupCount(),
        tiling.get_rowCount(), tiling.get_colCount(), tiling.get_innerLoopCount(), tiling.get_innerTailCount(),
        tiling.get_diagonal());

    // 以下代码是ascendebug必须的
    size_t* workspaces = context->GetWorkspaceSizes(1);
    workspaces[0] = 16 * 1024 * 1024;
    return ge::GRAPH_SUCCESS;
}
}  // namespace optiling

namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context) {
    const gert::Shape* x_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x_shape;
    return GRAPH_SUCCESS;
}
}  // namespace ge

namespace ops {
class Triu : public OpDef {
public:
    explicit Triu(const char* name) : OpDef(name) {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("diagonal").AttrType(OPTIONAL).Int(0);

        this->SetInferShape(ge::InferShape);

        this->AICore().SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend910b");
    }
};

OP_ADD(Triu);
}  // namespace ops
